package apkg;

import java.rmi.RMISecurityManager;
import java.util.UUID;

import javax.ejb.LocalBean;
import javax.ejb.Singleton;
import javax.servlet.http.HttpSession;

import net.jini.core.discovery.LookupLocator;
import net.jini.core.entry.Entry;
import net.jini.core.lookup.ServiceID;
import net.jini.core.lookup.ServiceRegistrar;
import net.jini.core.lookup.ServiceTemplate;
import net.jini.core.transaction.server.TransactionManager;
import net.jini.lookup.entry.Name;
import net.jini.space.JavaSpace;
import be.ac.ua.codebase.BankInterface;
import be.ac.ua.codebase.StoreConnectorInterface;

/**
 * Session Bean implementation class ServletBean
 */
@Singleton
@LocalBean
public class ServletBean {
	private String fReggie;
	private String fFirstName;
	private String fFamilyName;
	private int fPostalCode;
	private UUID fBankAccount;
	private String fBankId = null;

    /**
     * Default constructor. 
     */
    public ServletBean() {
        // TODO Auto-generated constructor stub
    }

    public String sayHello(String name) {
    	return "Dag " + name;
    }
    
    public void setup(String codebaseURL, String reggieURL, String firstName, String familyName, int postalCode) {
		System.setProperty("java.security.policy", "resources/policies.policy");
		System.setProperty("java.rmi.server.codebase", codebaseURL);
		System.setSecurityManager(new RMISecurityManager());
		
		fReggie = reggieURL;
		fFirstName = firstName;
		fFamilyName = familyName;
		fPostalCode = postalCode;
	}
    
	
	private BankInterface getBank() {
		try {
			System.out.println("Reggie url " + fReggie);
			LookupLocator lookup = new LookupLocator(fReggie); 
			ServiceRegistrar registrar = lookup.getRegistrar();
			
			if (fBankId == null) {
				System.out.println("search new bank");
				Class[] classes = new Class[] {BankInterface.class};
				BankInterface tmp = (BankInterface) registrar.lookup(new ServiceTemplate(null, classes, null));
				fBankId = tmp.getName();
				System.out.println("found1");
				return tmp;
			} else {
				System.out.println("try to find old bank");
				Entry[] attributes = new Entry[] {new Name(fBankId)}; 
				Class[] classes = new Class[] {BankInterface.class};
				UUID id = UUID.fromString(fBankId);
				ServiceID serviceId = new ServiceID(id.getMostSignificantBits(), id.getLeastSignificantBits());
				BankInterface tmp = (BankInterface) registrar.lookup(new ServiceTemplate(serviceId, null, null));
				System.out.println("found2");
				return tmp;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public void registerWithBank() {
		try {
			BankInterface tmp = getBank();
			fBankAccount = tmp.register(fFirstName, fFamilyName, fPostalCode);
		} catch(Exception e) {
			e.printStackTrace();
		}
	}
	
	public String getAmount() {
		try {
			BankInterface tmp = getBank();
			return Double.toString(tmp.getAmount(fBankAccount));
		} catch(Exception e) {
			e.printStackTrace();
		}
		return "-1";
	}
	
	public void changeAmount(String amountString) {
		try {
			BankInterface tmp = getBank();
			double amount = Double.parseDouble(amountString);
			if (amount > 0) {
				tmp.credit(fBankAccount, amount);
			} else {
				tmp.debit(fBankAccount, -amount);
			}
		} catch(Exception e) {
			e.printStackTrace();
		}
	}
}
